﻿using Simulation.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Easy.Game2d
{
    public class PlayerController : MonoBehaviour
    {
        #region DATA
        [SerializeField]
        private float _moveSpeed = 1;
        [SerializeField]
        private float _bulletSpeed = 1;

        private Rigidbody2D _rigidbody2D;
        private Coroutine _playerMoveCor;
        private int _damage = 1;
        #endregion

        #region SYSTEM
        private void Awake()
        {
            _rigidbody2D = GetComponent<Rigidbody2D>();
        }
        private void Start()
        {
            if (_playerMoveCor != null) StopCoroutine(_playerMoveCor);
            _playerMoveCor = StartCoroutine(WaitPlayerMove());
            StartCoroutine(WaitCreateBullet());

            EventUtil.AddListener(EventData.AddDamage, AddDamageClick);
        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                CreateHuoQiuBullet();
            }
            if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                CreateShanDianBullet();
            }
            if (_waitHuoQiu > 0) _waitHuoQiu -= Time.deltaTime;
            if (_waitShanDian > 0) _waitShanDian -= Time.deltaTime;
        }

        private void OnDestroy()
        {
            EventUtil.RemoveListener(EventData.AddDamage, AddDamageClick);
        }

        #endregion

        #region logic - player move
        private IEnumerator WaitPlayerMove()
        {
            while (true)
            {
                PlayerMove();
                yield return new WaitForFixedUpdate();
            }
        }
        private void PlayerMove()
        {
            float h = Input.GetAxisRaw("Horizontal");
            float v = Input.GetAxisRaw("Vertical");

            float posX = _rigidbody2D.position.x + h * Time.fixedDeltaTime * _moveSpeed;
            float posY = _rigidbody2D.position.y + v * Time.fixedDeltaTime * _moveSpeed;
            _rigidbody2D.position = new Vector2(posX, posY);
        }
        #endregion

        #region logic -player bullet
        private IEnumerator WaitCreateBullet()
        {
            while (true)
            {
                CreateBullet();
                yield return new WaitForSeconds(0.2f);
            }
        }
        private void CreateBullet()
        {
            if (Input.GetMouseButton(0))
            {
                // 获取鼠标坐标并转换成世界坐标
                Vector3 mousePosition = Input.mousePosition;
                mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
                // 因为是2D，用不到z轴。使将z轴的值为0，这样鼠标的坐标就在(x,y)平面上了
                mousePosition.z = 0;

                // 子弹角度
                float fireAngle = Vector2.Angle(mousePosition - this.transform.position, Vector2.up);
                if (mousePosition.x > this.transform.position.x)
                {
                    fireAngle = -fireAngle;
                }

                GameObject bullets = Resources.Load<GameObject>("Models/Bullets");
                GameObject bullet = Instantiate(bullets, transform.position, Quaternion.identity) as GameObject;
                bullet.GetComponent<Rigidbody2D>().velocity = ((mousePosition - transform.position).normalized * _bulletSpeed);
                bullet.transform.eulerAngles = new Vector3(0, 0, fireAngle);
                bullet.GetComponent<BulletItem>().damage = _damage;

            }
        }
        #endregion

        #region logic - jineng
        private float _waitHuoQiu = 0;
        private void CreateHuoQiuBullet()
        {
            if (_waitHuoQiu<=0)
            {
                _waitHuoQiu = 10;
                   // 获取鼠标坐标并转换成世界坐标
                   Vector3 mousePosition = Input.mousePosition;
                mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
                // 因为是2D，用不到z轴。使将z轴的值为0，这样鼠标的坐标就在(x,y)平面上了
                mousePosition.z = 0;

                // 子弹角度
                float fireAngle = Vector2.Angle(mousePosition - this.transform.position, Vector2.up);
                if (mousePosition.x > this.transform.position.x)
                {
                    fireAngle = -fireAngle;
                }

                GameObject bullets = Resources.Load<GameObject>("Models/BulletsHuoQiu");
                GameObject bullet = Instantiate(bullets, transform.position, Quaternion.identity) as GameObject;
                bullet.GetComponent<Rigidbody2D>().velocity = ((mousePosition - transform.position).normalized * _bulletSpeed);
                bullet.transform.eulerAngles = new Vector3(0, 0, fireAngle);
                bullet.GetComponent<BulletItem>().damage = _damage*2;
                SingleBehavior<PanelManager>.getInstance().Open<UIPlayStatePanle>(PanelName._playStatePanle).ShuaXinHuoQiu();
            }
        }

        private float _waitShanDian = 0;
        private void CreateShanDianBullet()
        {
            if (_waitShanDian <= 0)
            {
                _waitShanDian = 10;
                   // 获取鼠标坐标并转换成世界坐标
                   Vector3 mousePosition = Input.mousePosition;
                mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
                // 因为是2D，用不到z轴。使将z轴的值为0，这样鼠标的坐标就在(x,y)平面上了
                mousePosition.z = 0;

                // 子弹角度
                float fireAngle = Vector2.Angle(mousePosition - this.transform.position, Vector2.up);
                if (mousePosition.x > this.transform.position.x)
                {
                    fireAngle = -fireAngle;
                }

                GameObject bullets = Resources.Load<GameObject>("Models/BulletsShanDian");
                GameObject bullet = Instantiate(bullets, transform.position, Quaternion.identity) as GameObject;
                bullet.GetComponent<Rigidbody2D>().velocity = ((mousePosition - transform.position).normalized * _bulletSpeed);
                bullet.transform.eulerAngles = new Vector3(0, 0, fireAngle);
                bullet.GetComponent<BulletItem>().damage = _damage;

                SingleBehavior<PanelManager>.getInstance().Open<UIPlayStatePanle>(PanelName._playStatePanle).ShuaXinShanDian();
            }
        }
        #endregion

        #region damage
        // 带参数的点击事件
        private void AddDamageClick(EventArgs evt)
        {
            _damage += (int)evt.args[0];
            print(evt.type);
            print(evt.args[0]);
        }
        #endregion

        #region trigger
        private void OnCollisionEnter2D(Collision2D collision)
        {
            if (collision.gameObject.CompareTag("Monster"))
            {
                MonsterItem monsterItem = collision.gameObject.GetComponent<MonsterItem>();
                EventUtil.DispatchEvent("AddKouXueClick", monsterItem._hp);
                collision.gameObject.GetComponent<MonsterItem>().MonsterHide();
 
            }
        }
 
        #endregion
    }

}